//
//  HRStackView.swift
//  weiBo
//
//  Created by helong on 16/7/19.
//  Copyright © 2016年 helong. All rights reserved.
//

import UIKit

//  定义按钮类型的枚举
enum ButtonType: Int {
    case Picture = 0
    case Mention = 1
    case Trend = 2
    case Emotion = 3
    case Add = 4
}
class HRStackView: UIStackView {

    
    //  定义按钮点击的闭包
    var toolButtonClickedClosure: ((type: ButtonType) -> ())?
    //  记录表情按钮
    private var emotionButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     // MARK: - 设置UI
    private func setupUI(){
        addChildToolButton("compose_toolbar_picture", buttonType: .Picture)
        addChildToolButton("compose_mentionbutton_background", buttonType: .Mention)
        addChildToolButton("compose_trendbutton_background", buttonType: .Trend)
       emotionButton = addChildToolButton("compose_emoticonbutton_background", buttonType:.Emotion)
        addChildToolButton("compose_add_background", buttonType: .Add)
    }
    // MARK: - 创建底部按钮的方法
    private func addChildToolButton(imageName: String, buttonType: ButtonType) -> UIButton{
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named: "emoticon_keyboard_background"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "emoticon_keyboard_background"), forState: .Highlighted)

//        btn.sizeToFit()
        //  枚举转成原始值
        let rawValue = buttonType.rawValue
        btn.tag = rawValue
        //  取消高亮效果
        btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action: "toolButtonClicked:", forControlEvents: .TouchUpInside)
        addArrangedSubview(btn)
        return btn
    }

     // MARK: - 按钮的点击事件
    @objc private func toolButtonClicked(sender: UIButton){
        let type = ButtonType(rawValue: sender.tag)
        //  执行闭包传递被点击的按钮
        toolButtonClickedClosure?(type: type!)
        
    }
    
     // MARK: - 根据键盘的状态设置按钮的图标
    func setEmotionButtonImage(isEmotionKeyboard: Bool) {
        //  根据键盘的类型判断表情按钮的图标
        if isEmotionKeyboard {//如果是表情键盘显示键盘图标
            
            
            emotionButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emotionButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
            emotionButton?.adjustsImageWhenHighlighted = false
        }else {//如果不是表情键盘显示表情图标
            
            emotionButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emotionButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
            emotionButton?.adjustsImageWhenHighlighted = false
            
            
        }
    }
    
}
